//var cardUrl = "http://127.0.0.1:8080/card-client";
var DOMAIN = "http://127.0.0.1:8089/dis";
//var DOMAIN = "http://192.168.5.30:8089/dis";
var cardType_RL = "RL";
var cardType_SC = "SC";

var productFlg="";

/*时间汉化*/
$.fn.datetimepicker.dates['zh-CN'] = {
    days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
    daysShort: ["日", "一", "二", "三", "四", "五", "六", "日"],
    daysMin: ["日", "一", "二", "三", "四", "五", "六", "日"],
    months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
    monthsShort: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
    meridiem: ["上午", "下午"],
    today: "今天"
};

/**
 * 操作栏
 */
var funOprate = {
    'click .edit': function (e, value, row, index) {
        var $data = document.getElementById(e.target.dataset.edit).querySelectorAll('[data-data]');
        for (var i = 0; i < $data.length; i++) {
            $data[i].value = row[$data[i].dataset.data];
        };
    },
    'click .del': function (e, value, row, index) {
        var r = confirm("确定删除吗？");
        var delUrl = e.target.dataset.del;
        //  debugger;
        if (r) {
            $.ajax({
                type: "DELETE",
                url: delUrl + "&id=" + row.id,
                cache: false,
                async: false,
                success: function (data) {
                    alert("删除成功！");
                    if($("#tableApproach")){
                    	$("#tableApproach").bootstrapTable("refresh", {
                        silent: true //静态刷新
                    });
                    }
                    $("#table").bootstrapTable("refresh", {
                        silent: true //静态刷新
                    });
                }
            });
        }
    },
    'click .repeal': function (e, value, row, index) {
        var r = confirm("确定撤销吗？");
        var delUrl = e.target.dataset.del;
        //  debugger;
        if (r) {
            $.ajax({
                type: "DELETE",
                url: delUrl + "&id=" + row.id,
                cache: false,
                async: false,
                success: function (data) {
                    alert("撤销成功！");
                    if($("#tableApproach")){
                    	$("#tableApproach").bootstrapTable("refresh", {
                        silent: true //静态刷新
                    });
                    }
                    $("#table").bootstrapTable("refresh", {
                        silent: true //静态刷新
                    });
                }
            });
        }
    }
};
/**
 * 弹出层
 * @param eleId
 */
function openLayer(eleId) {
    layerIndex = layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        area: '500px auto',
        shadeClose: false,
        content: $('#' + eleId)
    });
    $('.layui-layer-content').css('overflow', 'hidden');
    return layerIndex;
};
/**
 * 关闭弹出层
 */
function closeLayer() {
    layer.close(layerIndex);
};

function isEmpty(obj) {
    if (obj == undefined || obj.length == 0 || obj == null || obj == "") {
        return true;
    }
    return false;
};
function isNotEmpty(obj) {
    return !isEmpty(obj);
};
//下拉框
var times = 0;
function getSelList(ele, dataType) {
    /*var url = DOMAIN + selUrl+isLogin;
    var proList = '<option value="">请选择</option>'
    $.ajax({
        type: 'get',
        contentType: 'application/json;charset=utf-8',
        url: url,
        dataType: 'json',

        success: function (res) {
            if (res.status == '401') {
                getSelList(ele, selUrl)
            } else {
                var data = res.data;
                for (var i = 0; i < data.length; i++) {
                    proList += '<option value = ' + data[i].code + '>' + data[i].name + '</option>'
                }
                ele.html(proList);
            }
        }

    })*/
   	var url=DOMAIN + "/v1/selList" + isLogin + "&dataType=" + dataType;
    //var proList = '<option value="">请选择</option>'
    var proList = '';
    $.ajax({
        type: 'get',
        contentType: 'application/json;charset=utf-8',
        url: url,
        dataType: 'json',

        success: function (res) {
           /* if (res.status == '401') {
                getSelList(ele, dataType);
            } else {*/
                var data = res.data;
                for (var i = 0; i < data.length; i++) {
                    proList += '<option value = ' + data[i].keyCode + '>' + data[i].keyDesc + '</option>'
                }
                ele.html(proList);
            //}
        },
        error: function(jqXHR, textStatus, errorThrown){
        	if(jqXHR.status=="401" && times<=3){
        		times++;
        		getSelList(ele, dataType);
        	}else{
        		times = 0;
        	}
        }

    });
};

function getSelList2(ele, dataType) {
   	var url=DOMAIN + "/v1/selList" + isLogin + "&dataType=" + dataType;
    var proList = '<option value="">请选择</option>'
    $.ajax({
        type: 'get',
        contentType: 'application/json;charset=utf-8',
        url: url,
        dataType: 'json',

        success: function (res) {
                var data = res.data;
                for (var i = 0; i < data.length; i++) {
                    proList += '<option value = ' + data[i].keyCode + '>' + data[i].keyDesc + '</option>'
                }
                ele.html(proList);
        },
        error: function(jqXHR, textStatus, errorThrown){
        	if(jqXHR.status=="401" && times<=3){
        		times++;
        		getSelList2(ele, dataType);
        	}else{
        		times = 0;
        	}
        }

    });
};

//多选框
function getCheList(ele, selUrl, val) {
    var proList = '';

    $.ajax({
        type: 'get',
        contentType: 'application/json;charset=utf-8',
        url: DOMAIN + selUrl,
        dataType: 'json',
        success: function (res) {
            var data = res.data;
            for (var i = 0; i < data.length; i++) {
                proList +=
                    '<div class="comparediv"><input type="checkbox" id="envinfo1_' + data[i].id + '" name="' + val + '" value="' + data[i].id + '"/>' +
                    '<label for="envinfo1_' + i + '">' + data[i].name + '</label></div>';
            }
            ele.html(proList);

        }
    })
};

/**
	 * 获取生成的编号
	 * @param codeName 编号名称，作为url的一部分 
	 * @param params 传参
	 * @returns
	 */
function getGeneratedCode(codeName) {
    var value = null;
    var url = DOMAIN + "/codeGen/" + codeName + isLogin;
    $.ajax({
        type: "GET",
        url: url,
        async: false,
        success: function (data) {
            value = data.data;
        }
    });

    return value;
};

// 把表单元素序列化的值转化为后台接口可以接收的对象
function getFormJsonObject($form) {
    var unindexed_array = $form.serializeArray();
    var indexed_array = {};

    $.map(unindexed_array, function (n, i) {
        indexed_array[n['name']] = n['value'];
    });

    return indexed_array;
};

function getToday() {
    var now = new Date();
    var h = now.getFullYear();
    var m = (now.getMonth() + 1 > 9) ? now.getMonth() + 1 : "0" + (now.getMonth() + 1);
    var s = (now.getDate() > 9) ? now.getDate() : "0" + now.getDate();
    var date = h + "-" + m + "-" + s;
    return date;
}

function loginOut() {
    mystorage.remove("test");
    mystorage.remove("loginName");
    mystorage.remove("userName");
    mystorage.remove("userCode");
    $.ajax({
        type: "DELETE",
        url: DOMAIN + '/v1/loginOut' + isLogin,
        success: function (result) {
            window.location.href = "login.html";
        },
    });

}

//判断新增或保存时名称是否重复
function checkRepeat(name, seurl) {
    var url = DOMAIN + '/v1' + seurl + isLogin;
    var flag = true;
    $.ajax({
        type: 'get',
        contentType: 'application/json;charset=utf-8',
        data: { name: name },
        url: url,
        dataType: 'json',
        async: false,
        success: function (res) {
            if (res.data.length <= 0) {
                flag = true;
            } else {
                flag = false;
            }
        }

    })
    return flag;
};

function buttonDisable() {
    $("input[type=button]").attr("disabled", "disabled");
    $("input[type=button]").css("opacity", "0.3");
    $("button").attr("disabled", "disabled");
    $("button").css("opacity", "0.3");
}

function buttonEnable() {
    $("input[type=button]").css("opacity", "1");
    $("input[type=button]").removeAttr("disabled");
    $("button").css("opacity", "1");
    $("button").removeAttr("disabled");
}

//通过价格表查询价格
function queryPrice(productCode){
	$.ajax({
			type: "get",
			url: DOMAIN + '/v1/queryPrice/' + isLogin,
			data: {"productCode":productCode},
			dataType: 'JSON',
			contentType: "application/JSON; charset=utf-8",
			success: function (result) {
				if (result.status == 200) {
					$("#price").val(result.data.price);
				}
			}
		});
}

function MouseWheel(event) {
	    event = event || window.event;
	    event.preventDefault();
}

var numberInput = $("input[type='number']");
	if(numberInput !=null && numberInput.length!= 0){
		for(var i=0;i<numberInput.length;i++){
		numberInput[i].addEventListener('DOMMouseScroll', MouseWheel, false);
		}
}